package com.watf.functions.gs;

import org.testng.Assert;
import org.testng.Reporter;

import com.thoughtworks.selenium.Selenium;
import com.watf.functions.core.DefaultBasicFuncs;
import com.watf.pages.gs.ActivityType;

public class ActivityTypeFuncs extends DefaultBasicFuncs {
	/*
	 * Create the local pairs with the values provided.
	 */
	public void newActivityType(Selenium selenium, String iActivtiyName,
			String iActivityType) throws Exception {

		selenium.click(ActivityType.New_BUTTON);
		selenium.waitForPageToLoad(CommonFuncs.SHORT_WAIT);

		selenium.type(ActivityType.Name_TEXT_FIELD, iActivtiyName);
		selenium.type(ActivityType.Description_TEXT_FIELD, iActivtiyName);

		selenium.click(iActivityType);
		selenium.click(ActivityType.Save_BUTTON);
		if(selenium.isAlertPresent())
		{
			selenium.getAlert();
			selenium.click(ActivityType.Cancel_BUTTON);
			selenium.waitForPageToLoad(CommonFuncs.SHORT_WAIT);
		} else
		{
			selenium.waitForPageToLoad(CommonFuncs.SHORT_WAIT);
		}

		Assert.assertTrue(this.isPresentInTable(selenium,
				ActivityType.Activity_TABLE, iActivtiyName));
	}

	//author Shenyang 2011-6-22
	public void ActivityTypeEdit(Selenium selenium, String iActivtiyName, String typeStr, String newActivityType) throws Exception {
		
		boolean selected = selectRadioButtonFromTable(selenium, ActivityType.Activity_TABLE, iActivtiyName);
        if (!selected)
        {
            Reporter.log("Cannot find a proper ActivityType to edit.");
            return;
        }
    
        try {
        	selenium.click(ActivityType.Edit_BUTTON);
	        selenium.waitForPageToLoad(CommonFuncs.SHORT_WAIT);
        	selenium.type(ActivityType.Description_TEXT_FIELD, typeStr);
	        selenium.click(newActivityType);
	        selenium.click(ActivityType.Save_BUTTON);
			selenium.waitForPageToLoad(CommonFuncs.SHORT_WAIT);
		} catch (Exception e) {
			Reporter.log(e.getMessage());
			selenium.waitForPageToLoad(CommonFuncs.SHORT_WAIT);
		}
        Assert.assertEquals(this.getColumnText(selenium,
				ActivityType.Activity_TABLE, iActivtiyName, 3), typeStr);
	}
	
	//author Shenyang 2011-6-22
	public void ActivityTypeRemove(Selenium selenium, String iActivtiyName) throws Exception {
		
		 boolean selected = selectRadioButtonFromTable(selenium, ActivityType.Activity_TABLE, iActivtiyName);
	        if (!selected)
	        {
	            Reporter.log("Cannot find a proper ActivityType to edit.");
	            return;
	        }

	        try {
	        	selenium.click(ActivityType.Remove_BUTTON);
	        	Assert.assertEquals((selenium.getConfirmation().matches("^Are you sure you want to remove this Activity[\\s\\S]$")),true);
		        selenium.waitForPageToLoad(CommonFuncs.SHORT_WAIT);
			} catch (Exception e) {
				selenium.waitForPageToLoad(CommonFuncs.SHORT_WAIT);
			}

	        Assert.assertEquals(this.isPresentInTable(selenium,
					ActivityType.Activity_TABLE, iActivtiyName), false);
		}
}